// pages/calendar/calendardemo.js
//引入封装请求
import request from "../../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: [],
    Idnumber: '',
    dateString: "",
    dateStrYMD: '',
    Id: "", //编号
    spot: [],
    man_hour: '', //每日工时为一值
    jitang: '',
    formData: {},
    studntName: '',
    WclassType: ["白班", "夜班"],
    WclassIndex: [],
    //准备提交数据
    officeHours: "", //上班时长
    overHours: "", //加班时长
    totalHours: "", //总时长 = 上班时长+加班时长
    officeHoursWage: "", //上班时薪
    overHoursWage: "", //加班班时薪
    salary: "", //工资 上班时长*上班时薪+加班时长*加班班时薪
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /* 取出登录信息 操作唯一值为身份证     字符串*/
    let userInfo = wx.getStorageSync('userInfo');
    //转换
    let UserInfo = JSON.parse(userInfo)
    /* 转完之后 存值当前页面  */
    this.setData({
      Idnumber: UserInfo[4],
      userInfo: UserInfo
    })
    //this.dateChange()
  },
    //表单项内容发生改变的回调
    handleInput(event) {
      //console.log(event);
      //let type = event.currentTarget.id;//id传值
      let type = event.currentTarget.dataset.type;
      //console.log(type,event.detail.value);
  
  
      //给初始化数据赋值
      this.setData({
        [type]: event.detail.value
      })
    },

  bindLeaTypeChange: function (e) {
    console.log('班别 发生选择改变，携带值为', e.detail.value);

    this.setData({
      WclassIndex: e.detail.value,
    })
  },




  async dateChange(e) {
    let that = this;
    //console.log(e)
    //console.log("选中日期变了,现在日期是", e.detail.dateString)
    //let dateStr=''+e.detail.year+e.detail.month+e.detail.day;
    let list = e.detail.dateString; //获取到的数据 单条
    //console.log(list);
    let dateStrYear = list.substring(0, 4); //要截取字段的字符串
    //console.log(dateStrYear)
    let dateStrMonth = list.substring(5, 7); //要截取字段的字符串
    //console.log(dateStrMonth)
    let dateStrDay = list.substring(8, 10); //要截取字段的字符串

    /* 取出登录信息 操作唯一值为身份证     字符串*/
    let userInfo = wx.getStorageSync('userInfo');
    let dateStrYMD = dateStrYear + dateStrMonth + dateStrDay;
    //转换
    let UserInfo = JSON.parse(userInfo)
    let Id = UserInfo[4] + dateStrYMD;
    let studntName = UserInfo[3];
    //获取每日工时为一值
    //查询请求
    let formData = await request('/WxInterface/WxSelectWorkTimeRecord', {
      serialNumber: Id,
      studentId: UserInfo[0]
    });
    that.setData({
      dateString: e.detail.dateString,
      dateStrYMD: dateStrYear + dateStrMonth + dateStrDay,
      formData: {},
      WclassIndex: [],
      totalHours: '',
      salary: '',
      man_hour: '',
      Id: Id,
      studntName:studntName
    });
    if (formData[0] != undefined) {
      that.setData({
        formData: formData[0],
        WclassIndex: formData[0].classType,
        totalHours: formData[0].totalHours,
        salary: formData[0].salary
      })
    } else {
      that.setData({
        officeHours: 0,
        officeHoursWage: 0,
        overHours: 0,
        overHoursWage: 0
      })
      wx.showToast({
        title: '辛苦了，加油。',
        icon: 'none',
        duration: 1800
      })
    }
  },

  /* 新增修改数据准备 */
  /* 获取总时长 上班时长*/
  // overHoursData(event){
  //   //  console.log(typeof event.detail.value);
  //   //  console.log(typeof this.data.overHours);
  //   let number =  Number(this.data.overHours);
  //   let number2 = Number(event.detail.value);
  //   // console.log(typeof number)
  //   // console.log(typeof number2)
  //   let totalHours = Number(number+number2);
  //   // console.log(typeof totalHours)
  //   // console.log(totalHours)
  //   this.setData({
  //     totalHours:totalHours
  //   })
  // },
  //   /* 获取总时长 加班时长*/
  // officeHoursData(event){
  //   // console.log(event.detail.value);
  //   // console.log(this.data.officeHours);
  //   let number =  Number(this.data.officeHours);
  //   let number2 = Number(event.detail.value);
  //   let totalHours = number+number2;
  //   console.log(totalHours)
  //   this.setData({
  //     totalHours:totalHours
  //   })
  // },

  /* 获取工资时长 统一计算*/
  officeHoursWageData(event) {
    let number1 = Number(this.data.officeHours); //上班时长
    let number2 = Number(this.data.officeHoursWage); //上班时薪
    let number3 = Number(this.data.overHours); //加班时长
    let number4 = Number(this.data.overHoursWage); //加班时薪

    if (number1 != 0 && number3 != 0) {
      let totalHours = number1 + number3;
      this.setData({
        totalHours: totalHours,
      })
    }
    if (number1 != 0 && number3 != 0 && number2 != 0 && number4 != 0) {
      let salary = number1 * number2 + number3 * number4;
      this.setData({
        salary: salary
      })
    }

  },
  // /* 获取工资 加班时薪 */
  // overHoursWageData(event){
  //   let number1 =  Number(this.data.officeHours);//上班时长
  //   let number2 =  Number(this.data.officeHoursWage);//上班时薪
  //   let number3 =  Number(this.data.overHours);//加班时长
  //   let number4 = Number(event.detail.value);//加班时薪

  //   let salary = number1*number2+number3*number4;

  //   console.log(salary)
  //   this.setData({
  //     salary:salary
  //   })

  // },


  /*新增OR修改*/
  formSubmit: async function (e) {
    //classType班别 officeHours加班时长 officeHoursWage加班时薪 overHours上班时长 overHoursWage上班时薪 salary工资  
    //serialNumber编号 textarea备注 totalHours总时长
    let {
      classType,
      officeHours,
      officeHoursWage,
      overHours,
      overHoursWage,
      salary,
      serialNumber,
      studntName,
      textarea,
      totalHours
    } = e.detail.value;


    // this.setData({
    //   classType, officeHours, officeHoursWage, overHours, overHoursWage,salary,serialNumber,studntName,remark:textarea,totalHours
    // })

    //新增or修改
    let formData = await request('/WxInterface/WxInsertAndUpdateWork', {
      classType,
      officeHours,
      officeHoursWage,
      overHours,
      overHoursWage,
      salary,
      serialNumber,
      studntName,
      remark: textarea,
      totalHours,
      studentId: this.data.userInfo[0]
    });

    console.log(formData);

    if (formData.State) {
      wx.showToast({
        title: formData.Text,
        icon: '',
        duration: 1800
      })
    } else {
      wx.showToast({
        title: formData.Text,
        icon: 'none',
        duration: 1800
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    let year = new Date().getFullYear();
    let month = new Date().getMonth() + 1;
    let day = new Date().getDate();

    let dateStrYMD = year + "" + month + "" + day;
    /* 取出登录信息 操作唯一值为身份证     字符串*/
    let userInfo = wx.getStorageSync('userInfo');
    //转换
    let UserInfo = JSON.parse(userInfo)
    let Id = UserInfo[4] + dateStrYMD;
    //一言https://v1.hitokoto.cn/?c=j&min_length=10&max_length=18&encode=text
    //api.hitokoto.cn
    wx.request({
      url: 'https://api.hitokoto.cn',
      method: 'get',
      data: {
        // c:'j',
        // min_length:'10',
        // max_length:'16',
        // encode:'text'
      },
      success: function (res) {
        //console.log(res.data.hitokoto)
        let jitang = res.data.hitokoto
        that.setData({
          jitang: jitang,
          Id: Id,
          studntName: UserInfo[3]
        })
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})